from rest_framework import serializers

from users.models import User

#获取查询的序列器
class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id','username','mobile','email')

# 增加用户的序列器
class UserAddSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id','username','mobile','email','password')
        extra_kwargs = {
        'password': {'max_length': 20,'min_length' : 8, 'write_only': True},
        'username': {'max_length': 20,'min_length' : 5}
        }


    # 重写create
    def create(self, validated_data):
        user = User.objects.create_user(**validated_data)
        return user